﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using DevExpress.Utils;
using SchoolManagementSystem.Report._2._ThuQuy;
using SchoolManagementSystem.Controls;
namespace SchoolManagementSystem.Forms.BusinessForm
{
    public partial class frmCashBook : DevExpress.XtraEditors.XtraForm
    {
        public frmCashBook()
        {
            InitializeComponent();

          
           // dateEdit_to.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
           // dateEdit_to.Properties.DisplayFormat.FormatString = "MM/yyyy";
           // dateEdit1_from.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
          //  dateEdit1_from.Properties.DisplayFormat.FormatString = "MM/yyyy";

            dateEdit1_from.DateTime = DateTime.Parse("1/1/" + DateTime.Now.Year);
            dateEdit_to.DateTime = DateTime.Now;

            string maskvn = "{0:0,0.###} đ";
            string maskusd = "${0:0,0.###} ";

            spinEdit_firstmoneyVND.Properties.DisplayFormat.FormatString = maskvn;
            spinEdit_firstmoneyVND.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
            spinEdit_firstmoneyVND.Properties.Mask.EditMask = "[0-9]+";
            spinEdit_firstmoneyVND.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;

            spinEdit_fisrtmoneyUSD.Properties.DisplayFormat.FormatString = maskusd;
            spinEdit_fisrtmoneyUSD.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
            spinEdit_fisrtmoneyUSD.Properties.Mask.EditMask = "[0-9]+";
            spinEdit_fisrtmoneyUSD.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;

          //  var defaulttondaukyvnd = RegisterService.RegisterServices.ConfigurationService.GetByCode("SoduDauKyVN");
           // var defaulttondaukyusd = RegisterService.RegisterServices.ConfigurationService.GetByCode("SoduDauKyUSD");
         //   var ghichusodudauky = RegisterService.RegisterServices.ConfigurationService.GetByCode("GhiChuSoDuDauKy");
          //  if (!String.IsNullOrEmpty(defaulttondaukyusd))
           // {
                //spinEdit_fisrtmoneyUSD.EditValue = int.Parse(defaulttondaukyusd);
           // }
           // if (!String.IsNullOrEmpty(defaulttondaukyvnd))
          //  {
           //     spinEdit_firstmoneyVND.EditValue = int.Parse(defaulttondaukyvnd);
           // }
            //if (!String.IsNullOrEmpty(ghichusodudauky))
           // {
           //     txtnote.Text = ghichusodudauky;
          //  }
            spinEdit_fisrtmoneyUSD.EditValue = 0;
            spinEdit_firstmoneyVND.EditValue = 0;
            txtnote.Text = "";

        }

        void ShowReport()
        {
            if (radioGroup1.SelectedIndex == 0)
            {
                ShowCashBack();
            }
            else {
                ShowCashBackFull();
            }
        }

        void ShowCashBack()
        {
            SoQuy ds = new SoQuy();
            ds.TonDauKyUSD = int.Parse(spinEdit_fisrtmoneyUSD.EditValue.ToString());
            ds.TonDauKyVND = int.Parse(spinEdit_firstmoneyVND.EditValue.ToString());
            string search ="";
           search += "PaymentMethod <> 2 and Convert(varchar(11), CreateDate, 120) BETWEEN '" + dateEdit1_from.DateTime.ToString("yyyy-MM-dd") + "' and '" + dateEdit_to.DateTime.ToString("yyyy-MM-dd") + "' and ( Excuter = 1) ";
           var data = SoQuyReportData.LoadCashBook(search,int.Parse(spinEdit_firstmoneyVND.EditValue.ToString()),int.Parse(spinEdit_fisrtmoneyUSD.EditValue.ToString()));        
           ds.DataSource = data;
           
           printControl1.PrintingSystem = ds.PrintingSystem;
           ds.CreateDocument();
        }
        void ShowCashBackFull()
        {
            SoQuyTongHop ds = new SoQuyTongHop();
            ds.TonDauKyUSD = int.Parse(spinEdit_fisrtmoneyUSD.EditValue.ToString());
            ds.TonDauKyVND = int.Parse(spinEdit_firstmoneyVND.EditValue.ToString());
            string search = "";
            search += " Convert(varchar(11), CreateDate, 120) BETWEEN '" + dateEdit1_from.DateTime.ToString("yyyy-MM-dd") + "' and '" + dateEdit_to.DateTime.ToString("yyyy-MM-dd") +"'";// and ( Excuter = 0 or Excuter =2 )";
            var data = SoQuyReportData.LoadCashBook(search, int.Parse(spinEdit_firstmoneyVND.EditValue.ToString()), int.Parse(spinEdit_fisrtmoneyUSD.EditValue.ToString()));
            ds.DataSource = data;

            printControl1.PrintingSystem = ds.PrintingSystem;
            ds.CreateDocument();
        }
        private void simpleButton_view_Click(object sender, EventArgs e)
        {
          
            ShowReport();
        }

        private void simpleButton_save_Click(object sender, EventArgs e)
        {
            var data = RegisterService.RegisterServices.ConfigurationService.GetOneByLINQ(c => c.Code == "SoduDauKyVN");
            var datausd = RegisterService.RegisterServices.ConfigurationService.GetOneByLINQ(c => c.Code == "SoduDauKyUSD");
            var note = RegisterService.RegisterServices.ConfigurationService.GetOneByLINQ(c => c.Code == "GhiChuSoDuDauKy");
            data.Value = spinEdit_firstmoneyVND.EditValue.ToString();
            datausd.Value = spinEdit_fisrtmoneyUSD.EditValue.ToString();
            note.Value = txtnote.Text;
            RegisterService.RegisterServices.ConfigurationService.Save(data);
            RegisterService.RegisterServices.ConfigurationService.Save(datausd);
            RegisterService.RegisterServices.ConfigurationService.Save(note);
            MessageControl.ShowMessage("Đã lưu số dư đầu kỳ này", "Thông báo", MessageControl.Icon.Success);
        }

        private void hyperLinkEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            
        }

        private void hyperLinkEdit1_OpenLink(object sender, DevExpress.XtraEditors.Controls.OpenLinkEventArgs e)
        {
            frmCashBookFirstPeriod c = new frmCashBookFirstPeriod();

            if (c.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                spinEdit_firstmoneyVND.EditValue = c.MoneyVN;
                spinEdit_fisrtmoneyUSD.EditValue = c.MoneyUSD;
                txtnote.Text = c.Note;
            }
            else
            {
                spinEdit_firstmoneyVND.EditValue = 0;
                spinEdit_fisrtmoneyUSD.EditValue = 0;
                txtnote.Text = "";
            }
        }


    }
   
        
        
}

